package com.zstu.mall.service.impl;

import com.zstu.mall.dao.ProductClassDao;
import com.zstu.mall.entity.Product;
import com.zstu.mall.entity.ProductActivity;
import com.zstu.mall.entity.ProductClass;
import com.zstu.mall.service.ProductActivityService;
import com.zstu.mall.service.UploadImageService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.zstu.mall.common.BaseServiceImpl;
import com.zstu.mall.service.ProductService;
import com.zstu.mall.dao.ProductDao;

import java.util.ArrayList;
import java.util.List;

/**
 * @author     : wuyc
 * @description: ProductServiceImpl
 * @date       : Created in 2019/11/24
 */
@Service
	public class ProductServiceImpl extends BaseServiceImpl<Product, String, ProductDao> implements ProductService {

	@Autowired
	private ProductDao productDao;

	@Autowired
	private ProductClassDao productClassDao;

	@Autowired
	private ProductActivityService productActivityService;

	@Autowired
	private UploadImageService uploadImageService;

	/**
	 * 查询
	 * @param id
	 * @return
	 */
	@Override
	public Product get(String id){
		Product entity =  this.productDao.getOne(id);
		//获取商品活动信息
		if(!entity.getActivityId().equals(" ")){
			String[] activityIds = entity.getActivityId().split(",");
			List<ProductActivity> productActivities = new ArrayList<>();
			for(int i=0; i < activityIds.length; i++){
				productActivities.add(productActivityService.get(activityIds[i]));
			}
			entity.setProductActivitys(productActivities);
		}
		//获取images
		if(!entity.getImage().equals(" ")){
			List<String> images = new ArrayList<>();
			String[] strings = entity.getImage().split(",");
			for(int i=0; i < strings.length; i++){
				if(strings[i].contains("."))
					images.add(strings[i]);
				else
					images.add(uploadImageService.get(strings[i]).getNewName());
			}
			entity.setImages(images);
		}
		return entity;
	}

	/**
	 * 通过标题搜索商品
	 *
	 * @param name
	 * @param pageable
	 * @return
	 */
	public List<Product> findByNameIsLike(String name, Pageable pageable){
		return productDao.findByNameIsLike(name, pageable);
	}

	/**
	 * 查找热门、精选商品
	 * 3热门,4精选,5轮播图,6促销
	 * @param status,pageable
	 * @return
	 */
	@Override
	public List<Product> findHotProduct(String status, Pageable pageable) {
		return productDao.findByStatusOrderByString1Desc(status, pageable);
	}

	/**
	 * 查找最新商品
	 *
	 * @param pageable
	 * @return
	 */
	@Override
	public List<Product> findNewProduct(Pageable pageable) {
		// 查找两周内上架的商品
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -14);
		return productDao.findNew(pageable);
	}

	/**
	 * 查找所有商品
	 *
	 * @param
	 * @return
	 */
	@Override
	public List<Product> findAll() {
		return productDao.findAll();
	}

	/**
	 * 根据分类查找商品
	 *
	 * @param type
	 * @param pageable
	 * @return
	 */
	@Override
	public List<Product> findByType(String type, Pageable pageable) {
		return productDao.findByType(type,pageable);
	}

	/**
	 * 根据活动查找商品
	 * @param activityId
	 * @param pageable
	 * @return
	 */
	@Override
	public List<Product> findByActivityId(String activityId,Pageable pageable){ return productDao.findByActivityId(activityId, pageable);}

	/**
	 * 排序
	 * @param sort
	 * @param type
	 * @param pageable
	 * @return
	 */
	public List<Product> findByOrder(Sort sort, String type, Pageable pageable){
		List<Product> products = new ArrayList<>();
		if(type.equals("0"))
			products = productDao.findAll(sort);
		else
			products = productDao.findByType(type, pageable);
		System.out.println(products);
		return products;
	}

	/**
	 * 剔除下架
	 * @param products
	 * @return
	 */
	public List<Product> findSoldOn(List<Product> products){
		List<Product> productList = new ArrayList<>();
		for(Product product : products){
			if(!product.getString1().equals("0"))
				productList.add(product);
		}
		return productList;
	}

}
